<?php
    session_start();
    require_once ('connection.inc.php');    
    
    // Take the values passed from the jQuery file and upload into MySQL
    /* ================================================================================
     * Product Quantity page
     */
    if ($_GET['p'] == 2) {
        $shop_id = SHOP_ID;
        $product_barcode = $_GET['barcode'];
        $product_quantity = $_GET['quant'];
        $employee_id = /*$_POST['employee_id']*/ "3";
        $time = date("Y-m-d H:i:s");

        // Retrieve the product ID for the current product
        $sql = "SELECT product_id FROM af1_products"
                . " WHERE product_barcode = $product_barcode";
        $result = mysqli_query($connection, $sql);
        $num_rows = mysqli_num_rows($result);
        if ($num_rows == 0) {
            echo "product not found";
            die();
        }
        $row = mysqli_fetch_assoc($result);
        $product_id = $row['product_id'];  

        if (empty($product_id) || empty($product_quantity)) {
            echo "Empty Error";
        }
        else {
            //Upload to MySQL
            $sql = "INSERT INTO af1_stock_check (timestamp, product_id, stock_level, employee_id, shop_id)"
                    . "VALUES ('$time', '$product_id', '$product_quantity', '$employee_id', '$shop_id')";
            $result = mysqli_query($connection, $sql);
            echo "success"; // Message picked up by jQuery callback
        }
    }
    
    /* ================================================================================
     * Add Product page
     */
    
    if ($_GET['p'] == 3) {
        $shop_id = SHOP_ID;
        $product_barcode = $_GET['barcode'];
        $product_title = $_GET['title'];
        $product_description = $_GET['desc'];
        $product_category = /*$_GET['cat']*/ 2;
        $pack_quantity = $_GET['quant'];
        $max_stock = $_GET['max'];
        $current_stock = $_GET['stock'];
        
        $sql = "INSERT INTO af1_products (product_id, product_barcode, product_title, product_description, product_cat_id, product_stock_level, product_pack_quantity, product_max_stock, shop_id)"
                    . "VALUES ('', '$product_barcode', '$product_title', '$product_description', '$product_category', '$current_stock', '$pack_quantity', '$max_stock', '$shop_id')";
        $result = mysqli_query($connection, $sql);
        echo "success";
           // echo "success"; // Message picked up by jQuery callback
    }    
?>